package com.ruoyi.nongjt.service.impl;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.nongjt.domain.NjtUserRegister;
import com.ruoyi.nongjt.mapper.NjtUserRegisterMapper;
import com.ruoyi.nongjt.service.INjtUserRegisterService;
import com.ruoyi.nongjt.stats.vo.BarPictureVO;
import com.ruoyi.nongjt.stats.vo.PiePictureVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 用户注册信息管理服务实现
 *
 * @author zhouyihao
 */
@Service
public class NjtUserRegisterServiceImpl implements INjtUserRegisterService {

    @Autowired
    private NjtUserRegisterMapper njtUserRegisterMapper;

    @Override
    public List<PiePictureVO> getMemberDistribution(Integer year) {
        // 查询指定年份的用户数据
        NjtUserRegister query = new NjtUserRegister();
        List<NjtUserRegister> users = njtUserRegisterMapper.selectNjtUserRegisterList(query);

        // 按会员状态分组统计
        Map<String, Long> distribution = users.stream()
                .filter(user -> user.getRegisterTime() != null &&
                        user.getRegisterTime().getYear() + 1900 == year)
                .collect(Collectors.groupingBy(NjtUserRegister::getMemberFlag, Collectors.counting()));

        // 转换为饼图VO
        List<PiePictureVO> result = new ArrayList<>();
        result.add(new PiePictureVO("会员", distribution.getOrDefault(1, 0L).intValue()));
        result.add(new PiePictureVO("非会员", distribution.getOrDefault(0, 0L).intValue()));

        return result;
    }

    @Override
    public BarPictureVO getMonthlyRegistration(Integer year) {
        // 查询指定年份的用户数据
        NjtUserRegister query = new NjtUserRegister();
        List<NjtUserRegister> users = njtUserRegisterMapper.selectNjtUserRegisterList(query);

        // 按月份分组统计
        Map<Integer, Long> monthlyData = users.stream()
                .filter(user -> user.getRegisterTime() != null &&
                        user.getRegisterTime().getYear() + 1900 == year)
                .collect(Collectors.groupingBy(
                        user -> user.getRegisterTime().getMonth() + 1,
                        Collectors.counting()
                ));

        // 构建月份数据
        List<String> categories = Arrays.asList(
                "1月", "2月", "3月", "4月", "5月", "6月",
                "7月", "8月", "9月", "10月", "11月", "12月"
        );

        List<Integer> values = new ArrayList<>();
        for (int i = 1; i <= 12; i++) {
            values.add(monthlyData.getOrDefault(i, 0L).intValue());
        }

        return new BarPictureVO(categories, values);
    }

    @Override
    public List<NjtUserRegister> selectNjtUserRegisterList(NjtUserRegister njtUserRegister) {
        return njtUserRegisterMapper.selectNjtUserRegisterList(njtUserRegister);
    }

    @Override
    public NjtUserRegister selectNjtUserRegisterById(Long id) {
        return njtUserRegisterMapper.selectNjtUserRegisterById(id);
    }

    @Override
    public int insertNjtUserRegister(NjtUserRegister njtUserRegister) {
        njtUserRegister.setCreateTime(new Date());
        njtUserRegister.setRegisterTime(new Date());
        njtUserRegister.setDelFlag("0");
        return njtUserRegisterMapper.insertNjtUserRegister(njtUserRegister);
    }

    @Override
    public int updateNjtUserRegister(NjtUserRegister njtUserRegister) {
        njtUserRegister.setUpdateTime(new Date());
        njtUserRegister.setUpdateBy(SecurityUtils.getUsername());
        return njtUserRegisterMapper.updateNjtUserRegister(njtUserRegister);
    }

    @Override
    @Transactional
    public int deleteNjtUserRegisterByIds(Long[] ids) {
        int count = 0;
        for (Long id : ids) {
            count += deleteNjtUserRegisterById(id);
        }
        return count;
    }

    @Override
    public int deleteNjtUserRegisterById(Long id) {
        NjtUserRegister njtUserRegister = new NjtUserRegister();
        njtUserRegister.setId(id);
        njtUserRegister.setUpdateTime(new Date());
        njtUserRegister.setUpdateBy(SecurityUtils.getUsername());
        return njtUserRegisterMapper.deleteNjtUserRegisterById(njtUserRegister);
    }
}